package jtk.project4.fleet.screen.inventory;

import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import jtk.project4.fleet.action.AddEmployeeAction;
import jtk.project4.fleet.action.AddEquipmentAction;
import jtk.project4.fleet.action.AddInventoryAction;
import jtk.project4.fleet.action.AddPartInventoryAction;
import jtk.project4.fleet.action.AddVendorAction;
import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CancelActionPartInventory;
import jtk.project4.fleet.action.CloseAction;
import jtk.project4.fleet.action.DeleteVendorAction;
import jtk.project4.fleet.action.EditInventoryAction;
import jtk.project4.fleet.action.EditVendorAction;
import jtk.project4.fleet.action.HelpAction;
import jtk.project4.fleet.action.InventoryCloseAction;
import jtk.project4.fleet.action.LastPmSetUpAction;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.action.SubmitActionPartInventory;
import jtk.project4.fleet.domain.Parts;
import jtk.project4.fleet.domain.Vendor;
import jtk.project4.fleet.event.InventoryEvent;
import jtk.project4.fleet.event.VendorEvent;
import jtk.project4.fleet.event.InventoryEvent.InventoryEventType;
import jtk.project4.fleet.event.VendorEvent.VendorEventType;
import jtk.project4.fleet.field.InventoryPartListingField;
import jtk.project4.fleet.field.InventoryViewOptionsField;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.action.ActionGroup;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.ComboField;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;
import nl.coderight.jazz.form.field.button.CheckButton;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;
import nl.coderight.jazz.i18n.Message;
import nl.coderight.jazz.i18n.Message.Choice;
import nl.coderight.jazz.i18n.Message.Param;
import nl.coderight.jazz.view.StatusBar;


public class InventoryView extends FormView{
	//private FormHeader formHeader;
	private TableField<Parts> inventoryTableField;
	private ComboField inventoryComboField;
	private PushButton addPartInventoryListingPushButton;
	private PushButton editPartInventoryListingPushButton;
	private PushButton deletePartInventoryListingPushButton;
	private PushButton printPartInventoryListingPushButton ;
	private PushButton filterPartInventoryListingPushButton;
	private CheckButton inventoryShowRowCheckButton ;
	
	private PushButton submitButton;
	private PushButton cancelButton;
	private PushButton generatePoButton;
	private PushButton closeButton;
	private PushButton helpButton; 
	private Action submitAction;
	private Action cancelAction;
	private Action helpAction;
	
	//private Action addInventoryAction;
	private Action addPartInventoryAction;
	private Action editInventoryAction;
	private Action deleteInventoryAction;
	private ActionGroup editInventoryActionGroup;
	
	private Action addEquipmentAction;
	private Action inventoryCloseAction;
	
	private Message status;
	private StatusBar statusBar;
	
	public InventoryView (){
		setSize(900,600);
		center();
	}
	
	
	
	@Override
	public void close() {
		// check if form is changed, if true show dialog
		if (isChanged()) {
			OptionDialog dialog = new OptionDialog(this);
			dialog.setMessageType(MessageType.QUESTION);
			dialog.setTitle("dialog.discard.title");
			dialog.setMessage("dialog.discard.message");
			dialog.setOptionType(OptionType.YES_NO);
			dialog.setOptions("button.yes", "button.no", "");
			dialog.show();
			if (dialog.getReturnOptionType() == ReturnOptionType.NO) {
				focus();
				return;
			}
		}
		super.close();
	}
	
	
	@Override
	protected void init() {
		createAction();
		createFields();
		createLayout();		
	}	
	
	private void createAction() {
		submitAction = new SubmitActionPartInventory();
		cancelAction = new CancelActionPartInventory();
		helpAction = new HelpAction();
		// enable submit button when form is changed
		setOnChangeAction(submitAction);
		
		addPartInventoryAction = new AddPartInventoryAction();
		editInventoryAction = new EditInventoryAction();
		deleteInventoryAction = new DeleteVendorAction();
		inventoryCloseAction = new InventoryCloseAction();
		
		editInventoryActionGroup = new ActionGroup();
		editInventoryActionGroup.addAction(editInventoryAction);
		editInventoryActionGroup.addAction(deleteInventoryAction);
		
	}
	
	private void createFields(){
		generatePoButton = new PushButton("add");
		closeButton = new PushButton(cancelAction);
		helpButton = new PushButton(helpAction);
		
		inventoryComboField = new ComboField("All Location","All Locations");
		inventoryTableField = new TableField("",""); 
			inventoryTableField.setAutoResizeMode(AutoResizeMode.OFF);
			
			inventoryTableField = new TableField<Parts>("parts", "part");
			inventoryTableField.setTitle("vendor.header.table");
			inventoryTableField.setAutoResizeMode(AutoResizeMode.OFF);
			inventoryTableField.getSelectionValues();			
			
			inventoryTableField.setOnSelectEvent(new InventoryEvent(InventoryEventType.SELECT));
			inventoryTableField.setOnChangeEvent(new InventoryEvent(InventoryEventType.CHANGE));
			inventoryTableField.setOnEditEvent(new InventoryEvent(InventoryEventType.EDIT));
			inventoryTableField.setOnSelectGroup(editInventoryActionGroup);	
			
			inventoryTableField.addColumn("label.Part","partId");
			inventoryTableField.addColumn("label.Name","name");
			inventoryTableField.addColumn("label.Description","desc");
			//inventoryTableField.addColumn("label.Manufacturer","");
		//	inventoryTableField.addColumn("label.Category","");
			inventoryTableField.addColumn("label.Unit Cost","unitCost");
			inventoryTableField.addColumn("label.Unit of Meas.","unitType");
			inventoryTableField.addColumn("label.Quantity","qty");
			//inventoryTableField.addColumn("label.Extended Cost","");
			//inventoryTableField.addColumn("label.PO #","");
			inventoryTableField.addColumn("label.Markup","markupType");
			inventoryTableField.addColumn("label.Warranty","warr");
		//	inventoryTableField.addColumn("label.Custom Field #2","");
			inventoryTableField.addColumn("label.UPC (barcode)","upcId");
			inventoryTableField.addColumn("label.Location","locId");
			//inventoryTableField.addColumn("label.Aisle","");
			//inventoryTableField.addColumn("label.Row","");
			//inventoryTableField.addColumn("label.Bin","");
			inventoryTableField.addColumn("label.Inventory Enabled","iEnabled");
			inventoryTableField.addColumn("label.Reorder Point","rPoint");
			inventoryTableField.addColumn("label.Reorder Quantity","rQty");
			inventoryTableField.addColumn("label.Lead Time","leadTime");
			
	//-------group buttons 2----------- pemasukan roh kedalam variabel wkwkwkwk
			//inventorySpinnerField = new SpinnerField("a","a");
			addPartInventoryListingPushButton = new PushButton(addPartInventoryAction);
			addPartInventoryListingPushButton.setToolTipText(addPartInventoryAction.getLongDescription());
			
			editPartInventoryListingPushButton = new PushButton(editInventoryAction);
			editPartInventoryListingPushButton.setToolTipText(editInventoryAction.getLongDescription());
			
			deletePartInventoryListingPushButton = new PushButton(deleteInventoryAction);
			deletePartInventoryListingPushButton.setToolTipText(deleteInventoryAction.getLongDescription());
			
			printPartInventoryListingPushButton= new PushButton("Print");
			filterPartInventoryListingPushButton= new PushButton("Filter");
			inventoryShowRowCheckButton = new CheckButton("Show Row Filter");
	
	}

	private GroupControl createFieldsGroup() {
		// TODO Auto-generated method stub
		GroupControl fieldGroup = new GroupControl();
		fieldGroup.addField(inventoryTableField, NORTH);
		fieldGroup.addField(createToolBar(),CENTER);
		fieldGroup.addField(createButtons(), SOUTH);
		return fieldGroup;
	}
	
	private GroupControl createToolBar() {
		// TODO Auto-generated method stub
		GroupControl buttonGroup = new GroupControl();
		buttonGroup.setLayout(new FlowLayout(FlowLayout.LEFT));
		buttonGroup.addField(addPartInventoryListingPushButton);
		buttonGroup.addField(editPartInventoryListingPushButton);
		buttonGroup.addField(deletePartInventoryListingPushButton);
		return buttonGroup;
	}
	

	private void createLayout() {
		//setHeader(createHeader());
		//addField(createForm());
	addField(createFieldsGroup(),CENTER);
	}

	

	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(generatePoButton);
		buttons.addField(helpButton);
		buttons.addField(closeButton);
		
		return buttons;
	}
	
	public void handleEvent(InventoryEvent evt){
		Parts selectedParts = inventoryTableField.getValue();
	
		switch(evt.getType()){
		case CLOSE:
			close();
		case CHANGE:
			//bwt filter
		case SELECT :
			ArrayList<Choice> choices = new ArrayList<Choice>();
			choices.add(new Choice(0, "status.message.none"));
			choices.add(new Choice(1, "status.message.one"));
			choices.add(new Choice(2, "status.message.all"));
			
			status = new Message("status.message");
			status.addParam(new Param(inventoryTableField.getValueCount()));
			status.addParam(new Param(inventoryTableField.getListOfValues().size(), choices));
			status.addParam(new Param(inventoryTableField.getSelectionCount()));
			// display message on status bar
			statusBar = new StatusBar();
			statusBar.setMessage(status);
	

		break;
		case DELETE:
			Message message = new Message("dialog.delete.message");
			message.addParam(new Param(selectedParts.getName()));
			message.addParam(new Param(selectedParts.getUdf1()));
			
			OptionDialog dialog = new OptionDialog(this);
			dialog.setTitle("dialog.delete.title");
			dialog.setOptionType(OptionType.YES_NO);
			dialog.setOptions("button.yes", "button.no", "button.cancel");
			dialog.setDefaultOption("button.no");
			dialog.setMessageType(MessageType.QUESTION);
			dialog.setMessage(message);
			dialog.show();			
			if (dialog.getReturnOptionType() == ReturnOptionType.YES) {
				propagateEvent(evt);
			}
			break;
		
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		

		}

	}

}
